package ru.papergames.model.proxies
{
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import mx.utils.URLUtil;
	import ru.papergames.controller.notifications.CoreNotifications;
	import ru.papergames.model.vo.PopupMessageVO;
	
	
	
	public class SettingsProxy extends BaseProxy
	{
		
		public static const NAME:String = 'SettingsProxy';
		
		public static const SETTINGS_LOAD_SUCCESS:String = 'SettingsProxy.SETTINGS_LOAD_SUCCESS';
		public static const SETTINGS_LOAD_ERROR:String = 'SettingsProxy.SETTINGS_LOAD_ERROR';
		
		public var rootURL:String = '';
		
		private var _settingsURL:String = 'settings.xml';
		private var _loader:URLLoader;
		
		private var _httpSession:String = '';
		
		private var _isDev:Boolean = false;
		
		public function SettingsProxy()
		{
			super(NAME, null);
		}
		
		protected function get settings():XML {
			return this.data as XML;
		}
		
		public function get httpSession():String {
			return _httpSession
		}
		
		public function set httpSession(val:String):void {
			_httpSession = val;
		}	
		
		public function get isDev():Boolean {
			return _isDev
		}
		
		public function set isDev(val:Boolean):void {
			_isDev = val;
		}
		
		override public function onRegister():void {
			super.onRegister();
			_loader = new URLLoader();
			_loader.addEventListener(Event.COMPLETE, onLoad);
			_loader.addEventListener(IOErrorEvent.IO_ERROR, onError);
			_loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
		}
		
		override public function onRemove():void {
			super.onRemove();
			_loader.removeEventListener(Event.COMPLETE, onLoad);
			_loader.removeEventListener(IOErrorEvent.IO_ERROR, onError);
			_loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
			_loader = null;
		}
		
		public function loadSettings(url:String = null):void {
			this._settingsURL = URLUtil.getFullURL(rootURL, url ? url : this._settingsURL);
			
			logger.debug('loadSettings from ' + this._settingsURL);
			
			try {
				
				_loader.load( new URLRequest(this._settingsURL) );
				
			} catch (e:Error) {
				
				facade.sendNotification(SETTINGS_LOAD_ERROR, e.message);
				facade.sendNotification(
					CoreNotifications.SYS_ERROR, 
					new PopupMessageVO(e.message, 'Ошибка загрузки настроек')
					);
				
			}
		}
		
		public function getNumberProperty(propName:String, defaultValue:Number = 0):Number {
			var propVal:String = getProperty(propName); 
			return propVal ? Number(propVal) : defaultValue;
		}
		
		public function getBooleanProperty(propName:String, defaultValue:Boolean = false):Boolean {
			var propVal:String = getProperty(propName); 
			return propVal ? propVal == 'true' : defaultValue;
		}
		
		public function getProperty(propName:String, defaultValue:String = null):String {
			if (!settings) return null;
			
			var propValue:String = settings.property.(@name == propName).toString();
			
			return propValue ? propValue : defaultValue;
		}
		
		protected function onLoad(evt:Event):void {
			//fill data
			try {
								
				this.data = new XML(_loader.data);				
				facade.sendNotification(SETTINGS_LOAD_SUCCESS);
				
			} catch (e:Error) {
				
				facade.sendNotification(SETTINGS_LOAD_ERROR, e.message);
				facade.sendNotification(
					CoreNotifications.SYS_ERROR, 
					new PopupMessageVO(e.message, 'Ошибка разбора файла настроек')
				);
				
			}		
			
		}
		
		protected function onError(evt:ErrorEvent):void {
			facade.sendNotification(SETTINGS_LOAD_ERROR, evt.text);
		}
	}
}